package com.unittec.zk.provider.auth.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.unittec.zk.provider.auth.entity.SysDic;
import com.unittec.zk.provider.auth.mapper.SysDicMapper;
import com.unittec.zk.provider.root.ServiceRoot;
import com.unittec.zk.provider.vo.VoCommonResult;
import com.unittec.zk.sdk.auth.request.Req101602;
import com.unittec.zk.sdk.auth.request.Req101605;
import com.unittec.zk.sdk.auth.request.Req101606;
import com.unittec.zk.sdk.auth.request.Req101614;

/**
 * @author Jason
 * @description 数据字典Service
 * @date 2019/11/07
 */
@Service
public class SysDicService extends ServiceRoot<SysDicMapper, SysDic> {

    /**
     * 增加字典列表
     *
     * @param sysDic
     * @return
     */
    public VoCommonResult saveDic(SysDic sysDic) {

        VoCommonResult voCommonResult = new VoCommonResult();
        voCommonResult.setIsSuccess(save(sysDic));
        voCommonResult.setPrimaryKey(sysDic.getId());
        return voCommonResult;
    }

    /**
     * 修改
     *
     * @param
     * @return
     */
    public VoCommonResult updateDic(Req101602 req101602, SysDic sysDic) {
        BeanUtils.copyProperties(req101602, sysDic);
        sysDic.setModifyTime(new Date());
        VoCommonResult voCommonResult = new VoCommonResult();
        voCommonResult.setIsSuccess(saveOrUpdate(sysDic));
        voCommonResult.setPrimaryKey(sysDic.getId());
        return voCommonResult;
    }

    /**
     * 获取字典详情
     *
     * @param
     * @return
     */
    public SysDic getDicDetail(String dicId) {
        return getById(dicId);
    }

    /**
     * 获取字典详情
     *
     * @param
     * @return
     */
    public SysDic getByDicCode(String dicCode) {
        LambdaQueryWrapper<SysDic> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDic::getDicCode, dicCode);
        List<SysDic> list = this.list(queryWrapper);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 字典列表
     *
     * @param
     * @return
     */

    public IPage<Map<String, Object>> getDicList(Req101605 req101605) {
        QueryWrapper<SysDic> qw = new QueryWrapper<SysDic>();
        qw.eq("del_flag", "0");
        if (StringUtils.isNotEmpty(req101605.getDicName())) {
            qw.like("dic_name", req101605.getDicName().toUpperCase());
        }
        if (StringUtils.isNotEmpty(req101605.getDicCode())) {
            qw.like("dic_code", req101605.getDicCode().toLowerCase());
        }
        if (StringUtils.isNotEmpty(req101605.getDescription())) {
            qw.like("description", req101605.getDescription());
        }

        return getMapper().dicList(new Page<SysDic>(req101605.getCurrent(), req101605.getSize()), qw);
    }

    public List<Map<String, Object>> getDicListNoPage(Req101606 req101606) {
        QueryWrapper<SysDic> qw = new QueryWrapper<SysDic>();
        qw.eq("del_flag", "0");
        if (StringUtils.isNotEmpty(req101606.getDicName())) {
            qw.like("dic_name", req101606.getDicName());
        }
        if (StringUtils.isNotEmpty(req101606.getDicCode())) {
            qw.like("dic_code", req101606.getDicCode());
        }
        if (StringUtils.isNotEmpty(req101606.getDescription())) {
            qw.like("description", req101606.getDescription());
        }
        return getMapper().dicListNo(qw);
    }

    /**
     * 字典名称和编码唯一性校验
     *
     * @param req101614
     * @return
     */
    public SysDic dicUnique(Req101614 req101614) {
        QueryWrapper<SysDic> qw = new QueryWrapper<SysDic>();
        qw.lambda().eq(SysDic::getDicCode, req101614.getDicCode());
        return getOne(qw);
    }
}
